﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["siakad"] != null && Session["siakad"] is SIAKADCORE.Models.Login)
            {
                Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)));
            }
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            string username = this.txtUsername.Text;
            string password = this.txtPassword.Text;

            var cridential = new SIAKADCORE.Models.Login();

            bool isSuccess = LoginController.Login(username, password, out cridential);
            if (isSuccess)
            {
                CreateSession(cridential);
                Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                Context.ApplicationInstance.CompleteRequest();
            }
            else
            {
                string script = "<script> $(document).ready(function (e) { shake(); });</script>";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Wrong", script, false);
            }
        }

        private void CreateSession(SIAKADCORE.Models.Login cridential)
        {
            Session["siakad"] = cridential;
        }
    }
}